import VueRouter from 'vue-router'
import Vue from 'vue'
import store from '@/store'
import { Message } from 'element-ui'
// 导入 四个页面
import layout from '@/views/layout'
import login from '@/views/login'
import dashboard from '@/views/dashboard'
import article from '@/views/article'

Vue.use(VueRouter)
// 设置 new一个 router
const router = new VueRouter({
  routes: [
    { path: '/login', component: login },
    {
      path: '/',
      component: layout,
      redirect: '/dashboard',
      children: [
        { path: '/dashboard', component: dashboard },
        { path: '/article', component: article }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (to.path !== '/login' && !token) {
    Message.warning('您还未登录！')
    return next('/login')
  }
  next()
})

export default router
